﻿//using AduSkin.Demo.UserControls;
using NewDeMo.Views;
using Prism.Ioc;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Controls;
using GrabFieldr.Models.Interface;
using GrabFieldr.Models.Impls.Tools;
using GrabFieldr.Views;




namespace NewDeMo.ViewModels
{
    public class MainWindowViewModel: BindableBase, INavigationAware
    {
        private string _title = "爱你哦";

        System.Timers.Timer DateTimer { get; set; }

        public string FormattedTime { get; set; }


        private string _test = "ggggggggggg";
        public string test
        {
            get { return _test; }
            set
            {
                _test = value;
                RaisePropertyChanged(nameof(test)); // 在setter中通知变更
            }
        }
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }
        IRegionManager regionManger;
        public IMachineModel machineModel;
        public MainWindowViewModel(IRegionManager regionManger, IMachineModel machineModel)
        {
            this.regionManger = regionManger;
            this.machineModel = machineModel;
            regionManger.RegisterViewWithRegion("ContentRegion", "ExtrPlat");
            regionManger.RegisterViewWithRegion("ContentRegion", "HomePage");
            //regionManger.RegisterViewWithRegion("ContentRegion", "PraticalCase");
            // 在构造函数中初始化命令
            Btn_Extr = new DelegateCommand<string>(Btn_Extr_Click);
            Btn_Home = new DelegateCommand<string>(Btn_Home_Click);
            DateTimer = new System.Timers.Timer(1000);
            machineModel._DateTime.PropertyChanged += ForTimePropertychanged; // 2. 订阅A类的属性变化事件
            RaisePropertyChanged(nameof(test));
            RaisePropertyChanged(nameof(FormattedTime));

            _PracticalCase = new PracticalCase(machineModel, regionManger);
        }

        // A属性改变时的事件处理程序
        private void ForTimePropertychanged(object sender, PropertyChangedEventArgs e)
        {
            // 检查发生变化的属性是否是B
            if (e.PropertyName == nameof(machineModel._DateTime.FormattedTime))
            {
                // 如果是B属性改变，则调用D方法，并传递事件参数
                ForTimeValueChange(sender, e);
            }
            // 可以根据需要处理其他属性的变化
        }

        // D方法：当A类的B属性改变时需要执行的操作
        public void ForTimeValueChange(object sender, PropertyChangedEventArgs e)
        {
            Console.WriteLine($"C类的D方法被调用！原因是A的B属性发生了变化。");
            Console.WriteLine($"属性名: {e.PropertyName}, 旧值: {e.OldValue}, 新值: {e.NewValue}");
            FormattedTime = machineModel._DateTime.FormattedTime;
            RaisePropertyChanged(nameof(FormattedTime)); // 4. 在属性设置器中调用通知方法
            // 这里可以添加任何需要执行的逻辑
        }
        /// <summary>
        /// 导航按键命令
        /// </summary>
        private DelegateCommand<string> _GoCommand;
        public DelegateCommand<string> GoCommand => _GoCommand ?? (_GoCommand = new DelegateCommand<string>(_GoCommand_Click));

        //private DelegateCommand<string> _Btn_Home;
        //public DelegateCommand<string> Btn_Home => _Btn_Home ?? (_Btn_Home = new DelegateCommand<string>(Btn_Home_Click));

        public ICommand Btn_Extr { get; private set; }
        public ICommand Btn_Home { get; private set; }

        /// <summary>
        /// 实用案例
        /// </summary>
        public UserControl _PracticalCase;
        public UserControl PracticalCase
        {
            get { return _PracticalCase; }
            set { SetProperty(ref _PracticalCase, value); }
        }





        public void Btn_Home_Click(string obj)
        {
            //Message = "我是ExtrPlat页面,点击了按钮";
            //var p = new NavigationParameters();
            //p.Add("data", 200);
            regionManger.RequestNavigate("ContentRegion", "HomePage");
        }

        public void Btn_Extr_Click(string obj)
        {
            //Message = "我是ExtrPlat页面,点击了按钮";
            //var p = new NavigationParameters();
            //p.Add("data", 200);
            regionManger.RequestNavigate("ContentRegion", "ExtrPlat");
        }

        private void _GoCommand_Click(string obj)
        {
            regionManger.RequestNavigate("ContentRegion", "HomePage");
        }

        //private void Btn_Home_Click(string obj)
        //{
        //    regionManger.RequestNavigate("ContentRegion", "HomePage");
        //}

        //private void Btn_Extr_Click(string obj)
        //{
        //    regionManger.RequestNavigate("ContentRegion", "ExtrPlat");
        //}

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            TimerInit();
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true; // 默认重用当前实例
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            DateTimer.Stop();
            DateTimer.Dispose();
        }

        /// <summary>
        /// 定时器初始化
        /// </summary>
        private void TimerInit()
        {
            // 绑定异步事件处理方法（注意使用async void）
            DateTimer.Elapsed += async (sender, e) => await OnTimedEventAsync();

            DateTimer.AutoReset = true; // 循环触发
            DateTimer.Enabled = true;   // 启动定时器
        }

        /// <summary>
        /// 异步任务方法（返回Task）
        /// </summary>
        private async Task OnTimedEventAsync()
        {
            // 获取当前时间并格式化为字符串（包含毫秒）
            DateTime currentTime = DateTime.Now;
            string formattedTime = currentTime.ToString("HH:mm:ss.fff"); // 24小时制，精确到毫秒
        }
    }
}
    

